package eBrain;

import java.io.IOException;
import java.text.ParseException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;

import org.apache.catalina.Session;
import org.apache.jasper.tagplugins.jstl.core.Redirect;

import com.sun.corba.se.spi.orbutil.fsm.Action;
import com.sun.xml.internal.bind.CycleRecoverable.Context;

import eBrain.model.ActionHandler;
import eBrain.model.BidWorker;
import eBrain.model.DataManager;
import eBrain.beans.*;

/**
 * Servlet implementation class eBrainServlet
 */
public class eBrainServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public eBrainServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	public void init(ServletConfig config) throws ServletException {
		System.out.println("*** initializing controller servlet.");
		super.init(config);

		DataManager dataManager = new DataManager();
		dataManager.setDbURL(config.getInitParameter("dbURL"));
		dataManager.setDbUserName(config.getInitParameter("dbUserName"));
		dataManager.setDbPassword(config.getInitParameter("dbPassword"));

		ActionHandler actionHandler = new ActionHandler();

		ServletContext context = config.getServletContext();
		context.setAttribute("base", config.getInitParameter("base"));
		context.setAttribute("imageURL", config.getInitParameter("imageURL"));
		context.setAttribute("dataManager", dataManager);
		context.setAttribute("actionHandler", actionHandler);

		try { // load the database JDBC driver

			Class.forName(config.getInitParameter("jdbcDriver"));
		} catch (ClassNotFoundException e) {
			System.out.println(e.toString());
		}
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String base = "/jsp/";
		String url = base + "index.jsp";
		String action = request.getParameter("action");
		if (action != null) {
			if (action.equals("search"))
				url = base + "SearchOutcome.jsp";
			else if (action.equals("selectCategory"))
				url = base + "SelectCategory.jsp";
			else if (action.equals("login"))
				url = base + "login.jsp";
			else if (action.equals("bid"))
				url = base + "setBid.jsp";
			else if (action.equals("authenticate"))
				url = base + "authenticate.jsp";
			else if (action.equals("addItem"))
				url = base + "addItem.jsp";
			else if (action.equals("myProfile"))
				url = base + ActionHandler.openUser(request);
			else if (action.equals("logout")) {
				ActionHandler.doLogout(request);
				url = base + "index.jsp";
			} else if (action.equals("openItem")) {
				ActionHandler.openItem(request);
				url = base + "item.jsp";
			} else if (action.equals("toAddUser"))
				url = base + "AddUser.jsp";
			else if (action.equals("addUser")) {
				try {
					url = base + ActionHandler.addUser(request);
				} catch (ParseException e) {
					url = base + "userVal.jsp";
					e.printStackTrace();
				}
			} else if (action.equals("salestat")) {
				url = base + ActionHandler.openSalesStat(request);
			} else if (action.equals("meBrain")) {
				if (ActionHandler.isLoggedIn(request)) {
					ActionHandler.doSales(request);
					url = base + "meBrain.jsp";
				}
			} else {
				url = base + "index.jsp";
			}
		}
		RequestDispatcher requestDispatcher = getServletContext()
				.getRequestDispatcher(url);
		requestDispatcher.forward(request, response);
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		String base = "/jsp/";
		String url = base + "index.jsp";
		String action = request.getParameter("action");
		if (action != null) {
			if (action.equals("storeBid")) {
				url = base + ActionHandler.storeBid(request);
			} else if (action.equals("updateItem")) {
				url = base + "itemstore.jsp";
			} else if (action.equals("storeItem")) {
				url = base + ActionHandler.storeItem(request);
			} else if (action.equals("updateUser")) {
				try {
					url = base + ActionHandler.updateUser(request);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					url = base + "index.jsp";
				}
			} else if (action.equals("addUser")) {
				try {
					url = base + ActionHandler.addUser(request);
				} catch (ParseException e) {
					e.printStackTrace();
					url = base + "index.jsp";
				}
			} else if (action.equals("salestat")) {
				url = base + ActionHandler.openSalesStat(request);
			}
			if (action.equals("dologin")) {
				url = base + ActionHandler.doLogin(request);
			}
			RequestDispatcher requestDispatcher = getServletContext()
					.getRequestDispatcher(url);
			requestDispatcher.forward(request, response);

		}
	}

}
